<?php

namespace app\store\model;

use app\common\model\gifts\UserGifts as UserGiftsModel;
/**
 * 订单管理
 * Class Order
 * @package app\store\model
 */
class UserGifts extends UserGiftsModel
{


    /**
     * 获取用户购买的爆品礼列表
     * @param $user_id
     * @param bool $is_use 是否已使用
     * @param bool $is_expire 是否已过期
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($query = [])
    {
        // 检索查询条件
        !empty($query) && $this->setWhere($query);
        
        // 获取数据列表
        return $this->with(['image', 'user'])
            ->alias('gifts')
            ->join('user', 'gifts.user_id = user.user_id')
            ->where(['gifts.is_delete'=>0,'gifts.type'=>1])
            ->order(['gifts.create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);

    }


    /**
     * 设置检索查询条件
     * @param $query
     */
    private function setWhere($query)
    {

        if (isset($query['search']) && !empty($query['search'])) {
            $this->where('gifts.gifts_title|user.nickName', 'like', '%' . trim($query['search']) . '%');
        }

        if (isset($query['start_time']) && !empty($query['start_time'])) {
            $this->where('gifts.create_time', '>=', strtotime($query['start_time']));
        }
        if (isset($query['end_time']) && !empty($query['end_time'])) {
            $this->where('gifts.create_time', '<', strtotime($query['end_time']) + 86400);
        }
        if (isset($query['status']) && !empty($query['status'])) {
            $query['status'] > -1 && $this->where('gifts.is_expire', '=', $query['status']);
        }


    }





}
